<?php

namespace app\outstockentry\controller;
use think\Db;
use think\Request;
use think\Exception;

class Index extends \AbstractController
{
    

    public function index(){

        die('111');

    }

    public function addoutstockentry()
    {
        $str = "abcdefg062";
        preg_match_all("/\d+/",$str,$num);
        $num = $num[0][count($num[0])-1];
        $new_num = sprintf("%0".(strlen($num))."d",$num+1);
        $str = str_replace($num,$new_num,$str);

        header("Content-type:text/html;charset=utf-8");
       // $postData = $this->request->post();

        $postData = request()->param();

        var_dump($postData);die;

        /*$postData=[
        'FBILLTYPEID'=>'标准销售',
        'FDATE'=>'2019/8/145555',
        'currency'=>'人民币',
        'FSALEORGID'=>'0002',
        'FMATERIALID'=>[
            '0'=>'D.11.1.004',
            '1'=>'D.11.1.005',
            '2'=>'D.11.1.006'
        ],
        'FREALQTY'=>[
            '0'=>'2',
            '1'=>'3',
            '2'=>'4'
        ],
        'FNOTE'=>[
            '0'=>'备注1',
            '1'=>'备注2',
            '2'=>'备注3'
        ]
    ];*/
        /*$postData=[
            'FBILLTYPEID'=>'标准销售',
            'FDATE'=>'2019/8/145555',
            'currency'=>'人民币',
            'FSALEORGID'=>'0002',
            'datalist'=>[
                '0'=>['FMATERIALID'=>'D.11.1.007',
                    'FREALQTY'=>'6',
                    'FNOTE'=>'备注5'
                ],
                '1'=>['FMATERIALID'=>'D.11.1.008',
                    'FREALQTY'=>'7',
                    'FNOTE'=>'备注6'
                ],
                '2'=>['FMATERIALID'=>'D.11.1.009',
                    'FREALQTY'=>'8',
                    'FNOTE'=>'备注7'
                ],
            ]
        ];*/



        $indexModel = new \app\outstockentry\model\Index();
        $data = $indexModel->getoutstockentry();

        if(empty($data)){
            $postData['FBILLNO']='XSCKD000001';
            //echo '111';die;//没数据
        }else{
            $str= $data[0]['FBILLNO'];
            preg_match_all("/\d+/",$str,$num);
            $num = $num[0][count($num[0])-1];
            $new_num = sprintf("%0".(strlen($num))."d",$num+1);
            $str = str_replace($num,$new_num,$str);
            $postData['FBILLNO']=$str;
           // echo $str;die;//有数据
        }
        $insert=[
            'FBILLTYPEID'=>$postData['FBILLTYPEID'],
            'FDATE'=>$postData['FDATE'],
            'currency'=>$postData['currency'],
            'FBILLNO'=>$postData['FBILLNO']

        ];

        Db::startTrans();
        try {
            $res1 = $indexModel->addOutstock($insert);
            if($res1>0){
                /*foreach ($postData['FMATERIALID'] as $k=>$v){
                    $data[$k]=[
                        'FMATERIALID'=>$postData['FMATERIALID'][$k],
                        'FREALQTY'=>$postData['FREALQTY'][$k],
                        'FNOTE'=>$postData['FNOTE'][$k],
                        'FID'=>$res1
                    ];
                }*/
                foreach ($postData['datalist'] as $k=>&$v){
                    /*$data[$k]=[
                        'FMATERIALID'=>$postData['FMATERIALID'][$k],
                        'FREALQTY'=>$postData['FREALQTY'][$k],
                        'FNOTE'=>$postData['FNOTE'][$k],
                        'FID'=>$res1
                    ];*/
                    $postData['datalist'][$k]['FID']=$res1;
                }

                $data=$postData['datalist'];
                $res2 =  $indexModel->addOutstockentry($data);

            }
            return json_encode(["msg" => "添加成功", "state" => "1"]);
        } catch (\Exception $e) {
           // return $e->getMessage();die;
            Db::rollBack();
            return json_encode(["msg" => "系统繁忙请稍后再试2", "state" => "2"]);
        }

    }


    //删除销售出库单
    public function delete()
    {
        $params = request()->param();
       // var_dump($params);die;
        $id=$params['id'];
        $indexModel = new \app\outstockentry\model\Index();
        Db::startTrans();
        try {
            $res1 = $indexModel->delOutstock($id);
            if($res1>0){
                $res2 =  $indexModel->delOutstockentry($id);
                return json_encode(["msg" => "删除成功", "state" => "1"]);
            }else{
                return json_encode(["msg" => "删除成功2", "state" => "1"]);
            }

        } catch (\Exception $e) {
            // return $e->getMessage();die;
            Db::rollBack();
            return json_encode(["msg" => "系统繁忙请稍后再试", "state" => "2"]);
        }

    }

    public function getlist(){
        $indexModel = new \app\outstockentry\model\Index();
        $res1 = $indexModel->getlist();
        return json_encode(["msg" => "数据列表", "state" => "1","data" => $res1]);
    }


public function stringg()
{
  //  $url="ftp://ud03:password@s.jb51.net/中文/中文.rar";

    $url="{'OrderCode':'','ShipperCode':'SF','LogisticCode':'118954907573'}";
    //$url="OrderCode=''&ShipperCode='SF'&LogisticCode='118954907573'";
    $url = urlencode(mb_convert_encoding($url, 'gb2312', 'utf-8'));
    return $url;



 /*   $a = array("%3A", "%2F", "%40");
    $b = array(":", "/", "@");
    $url = str_replace($a, $b, $url);
    return $url;*/

}

public function dd(){
    $url = "http://v.juhe.cn/toutiao/index";;
    $post_data = array ("type" => "shehui","key" => "db05e1234f593a7b458b43c3bfb2a000");
// $paramstring = http_build_query($params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
    curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $output = json_decode(curl_exec($ch),true);
    curl_close($ch);
//打印获得的数据
// print_r($output);
    $news=$output['result']['data'];
// print_r($news);
    var_dump($news);
}


    public function ff(){
        $url = "http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json";
        $url = "http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json?EBusinessID=test1565128&DataType=2&RequestType=1107&RequestData=%7b%22ShipperCode%22%3a%22ZTO%22%2c%22StationCode%22%3a%221234561%22%2c%22StationName%22%3a%22%E8%A5%BF%E4%B9%A1%E7%BD%91%E7%82%B9%22%2c%22ApplyID%22%3a%221237100%22%2c%22Company%22%3a%22%E5%BF%AB%E9%80%92%E9%B8%9F%22%2c%22Name%22%3a%22hoo123%22%2c%22Tel%22%3a%220755-8812345%22%2c%22Mobile%22%3a%2215612344567%22%2c%22ProvinceName%22%3a%22%E5%B9%BF%E4%B8%9C%E7%9C%81%22%2c%22ProivnceCode%22%3a%22440000%22%2c%22CityName%22%3a%22%E6%B7%B1%E5%9C%B3%E5%B8%82%22%2c%22CityCode%22%3a%22440300%22%2c%22ExpAreaName%22%3a%22%E7%A6%8F%E7%94%B0%E5%8C%BA%22%2c%22ExpAreaCode%22%3a%22%22%2c%22Address%22%3a%22%E8%A5%BF%E4%B9%A11%E8%B7%AF%22%7d&DataSign=YTI5OTRmZTI4ODEwYjA4ODY5ZWZmNjFiZDc1NjIxMzU=";
        $post_data = array ("EBusinessID" => 'test1565128&DataType=2&RequestType=1107&RequestData=%7b"ShipperCode"%3a"ZTO"%2c"StationCode"%3a"1234561"%2c"StationName"%3a"西乡网点"%2c"ApplyID"%3a"1237100"%2c"Company"%3a"快递鸟"%2c"Name"%3a"hoo123"%2c"Tel"%3a"0755-8812345"%2c"Mobile"%3a"15612344567"%2c"ProvinceName"%3a"广东省"%2c"ProivnceCode"%3a"440000"%2c"CityName"%3a"深圳市"%2c"CityCode"%3a"440300"%2c"ExpAreaName"%3a"福田区"%2c"ExpAreaCode"%3a""%2c"Address"%3a"西乡1路"%7d&DataSign=YTI5OTRmZTI4ODEwYjA4ODY5ZWZmNjFiZDc1NjIxMzU=');
// $paramstring = http_build_query($params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
        curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, []);
        $output = json_decode(curl_exec($ch),true);
        var_dump($output);die;

        curl_close($ch);
//打印获得的数据
// print_r($output);
        $news=$output['result']['data'];
// print_r($news);
        var_dump($news);
    }








}
